package edu.sctu.practice.class01.zhengsenwen.practice;

import java.util.Scanner;

/**
 * Created by zhengsenwen on 2016/1/10.
 */
public class YearsToKnow {

    private static boolean isLeapYear(int year) {
        if (year % 100 == 0) {
            if (year % 400 == 0) {
                return true;
            }
        } else {
            if (year % 4 == 0) {
                return true;
            }
        }
        return false;
    }

    private static boolean isOutOfRange(int year) {
        return year > 9999 || year < -10000;
    }

    private static int scannerYear() {
        Scanner scanner = new Scanner(System.in);
        String number = scanner.nextLine();

        if (!number.matches("^+?[1-9][0-9]*$")) {
            System.out.println("Not an integer number! Input again:");
            return scannerYear();
        } else {
            int year = Integer.parseInt(number);
            if (isOutOfRange(year)) {
                System.out.println("Unusual year! Another please:");
                return scannerYear();
            }
            return year;
        }
    }

    public static void main(String[] args) {

        System.out.println("Please input a year as an integer nubmer:");

        int year = scannerYear();

        System.out.print("The year of " + year + " is" + (isLeapYear(year) ? "" : " not") + " a leap year.");
    }

}
